Page({
  data: {
    list: [],
    key: "",
    role: null
  },
  onLoad(e) {

    console.log(e)
    this.setData({ role: e.role })
    this.getEmp()
  },
  sltback(e) {
    console.log(e)
    var preview = getCurrentPages()[getCurrentPages().length - 2]
    var newinfo = preview.data.info
    console.log(newinfo)
    console.log(this.data.list)



    var v = e.detail.value;
    var k = ""
    for (const i in this.data.list) {
      if (this.data.list[i].EmployeeId == v) {
        k = this.data.list[i].EmployeeName
      }
    }

    if (newinfo.Customer) {
      if (this.data.role == "Employee") {
        newinfo.Customer.EmployeeId = v
        newinfo.Customer.Employee.EmployeeName = k
      } else if (this.data.role == "Source") {
        newinfo.RecommendNo = v
        newinfo.RecommendCustomer = { CustomerName: k }
      } else if (this.data.role == "SoftDesign") {
        newinfo[this.data.role + "Id"] = v
        newinfo[this.data.role + "er"].EmployeeName = k
      } else if (this.data.role == "Measure") {
        newinfo[this.data.role + "Id"] = v
        newinfo[this.data.role + "Name"] = k
      } else {
        newinfo[this.data.role + "Id"] = v
        newinfo[this.data.role].EmployeeName = k
      }
    } else {
      if (this.data.role == "Employee") {
        newinfo.EmployeeId = v
        newinfo.EmployeeName = k
      } else if (this.data.role == "Source") {
        newinfo.RecommendNo = v
        newinfo.RecommendCustomer = { CustomerName: k }
      } else if (this.data.role == "SoftDesign") {
        newinfo[this.data.role + "Id"] = v
        newinfo[this.data.role + "er"].EmployeeName = k
      } else if (this.data.role == "Measure") {
        newinfo[this.data.role + "Id"] = v
        newinfo[this.data.role + "Name"] = k
      } else {
        newinfo[this.data.role + "Id"] = v
        newinfo[this.data.role].EmployeeName = k
      }
    }


    //   var v = e.detail.value;
    //   var k = ""
    //   for (const i in this.data.list) {
    //     if (this.data.list[i].EmployeeId == v) {
    //       k = this.data.list[i].EmployeeName
    //     }
    //   }

    // var v = e.detail.value;
    // var k = ""
    // for (const i in this.data.list) {
    //   if(this.data.list[i].EmployeeId == v){
    //     k = this.data.list[i].EmployeeName
    //   }
    // }

    // if(this.data.role == "Employee"){
    //   newinfo.Customer.EmployeeId = v
    //   newinfo.Customer.Employee.EmployeeName = k
    // }else if(this.data.role == "Source"){
    //   newinfo.RecommendNo = v
    //   newinfo.RecommendCustomer = {CustomerName:k}
    // }else if(this.data.role == "SoftDesign"){
    //   newinfo[this.data.role + "Id"] = v
    //   newinfo[this.data.role + "er"].EmployeeName = k
    // }else if(this.data.role == "Measure"){
    //   newinfo[this.data.role + "Id"] = v
    //   newinfo[this.data.role + "Name"] = k
    // }else{
    //   newinfo[this.data.role + "Id"] = v
    //   newinfo[this.data.role].EmployeeName = k
    // }

    // var v = e.detail.value;
    // var k = ""
    // for (const i in this.data.list) {
    //   if (this.data.list[i].EmployeeId == v) {
    //     k = this.data.list[i].EmployeeName
    //   }
    // }

    // if (this.data.role == "Employee") {
    //   newinfo.Employee.EmployeeId = v
    //   newinfo.Employee.EmployeeName = k
    // } else if (this.data.role == "Source") {
    //   newinfo.RecommendNo = v
    //   newinfo.RecommendCustomer = { CustomerName: k }
    // } else if (this.data.role == "SoftDesign") {
    //   newinfo[this.data.role + "Id"] = v
    //   newinfo[this.data.role + "er"].EmployeeName = k
    // } else if (this.data.role == "Measure") {
    //   newinfo[this.data.role + "Id"] = v
    //   newinfo[this.data.role + "Name"] = k
    // } else {
    //   newinfo[this.data.role + "Id"] = v
    //   newinfo[this.data.role].EmployeeName = k
    // }


    //  var a = e.detail.value;
    //     var b = ""
    //     for (const i in this.data.list) {
    //       if(this.data.list[i].EmployeeId == a){
    //         b = this.data.list[i].EmployeeName
    //       }
    //     }

    //     if(this.data.role == "Employee"){
    //       newinfo.Employee.EmployeeId = a
    //       newinfo.Employee.EmployeeName =b
    //     }else if(this.data.role == "Source"){
    //       newinfo.RecommendNo = a
    //       newinfo.RecommendCustomer = {CustomerName:b}
    //     }else if(this.data.role == "SoftDesign"){
    //       newinfo[this.data.role + "Id"] = a
    //       newinfo[this.data.role + "er"].EmployeeName = b
    //     }else if(this.data.role == "Measure"){
    //       newinfo[this.data.role + "Id"] = a
    //       newinfo[this.data.role + "Name"] = b
    //     }else{
    //       newinfo[this.data.role + "Id"] = a
    //       newinfo[this.data.role].EmployeeName = b
    //     }






    preview.setData({ info: newinfo })

    console.log(newinfo)





    dd.navigateBack()





  },






  getEmp(e) {
    var _this = this
    getApp().req({
      url: "/user/select",
      data: {
        keyword: this.data.key
      },
      cb(res) {

        console.log(res)
        _this.setData({
          list: res.data
        })
      }
    })
  },
  searchKey(e) {
    this.setData({ key: e.detail.value })
    this.getEmp()
  },
  closekey() {
    dd.hideKeyboard();
  }
});
